/*
	Abr12Brush.m
        Copyright 2008 Laura Dickey.  All rights reserved.

        This file is part of BrushViewQL.

        BrushViewQL is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 2 of the License, or
        (at your option) any later version.

        BrushViewQL is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with BrushViewQL; if not, write to the Free Software
        Foundation, Inc., 59 Temple Place, Suite 330,
        Boston, MA  02111-1307  USA

        You may contact the author at: brushviewql@gmail.com
*/

#import "Abr12Brush.h"
#import "BrushFile.h"

@implementation Abr12Brush

#define READ_LONG(val) \
	val = *(long *)brushData;\
	val = CFSwapInt32BigToHost(val);\
	brushData += sizeof(val);
	
#define READ_SHORT(val) \
	val = *(short *)brushData;\
	val = CFSwapInt16BigToHost(val);\
	brushData += sizeof(val);

- (NSImage *) loadData:(unsigned char *)brushData version:(int)vers
{
	NSImage	*theImage;
	int	i, width, height;
	long	nameLen;
	short	shortBounds[4];
	long	longBounds[4];
	
	READ_LONG(misc);
	READ_SHORT(spacing);
	if (vers == 2) {
		READ_LONG(nameLen);
		name = [[NSString alloc]initWithBytes:brushData length:nameLen*2
			encoding:NSUnicodeStringEncoding];
		[name retain];
		brushData += (nameLen * 2);
	//	BRLog(@"reading 12 brush named %@", name);
	}
	
	antiAliasing = *(char *)brushData++;
	for (i = 0; i < 4; i++) {
		READ_SHORT(shortBounds[i]);
	}
	for (i = 0; i < 4; i++) {
		READ_LONG(longBounds[i]);
	}
	READ_SHORT(depth);
	height = longBounds[2] - longBounds[0];
	width = longBounds[3] - longBounds[1];
	
	if ((longBounds[0] - longBounds[2]) > 16384) {
		// if height is bigger then this is a "wide brush"
	//	BRLog(@"Wide Brush!!");
	}
	compressed = *(char *)brushData++;
	theImage = [BrushFile createImage:&brushData
		width:width height:height depth:depth compressed:compressed];
	return theImage;
}

@end
